package com.jt.service;
import com.jt.vo.ImageVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service   //service 层
public class FileServiceImpl implements FileService{

    /**
     * 业务说明:
     *  1.校验图片的类型 是否为 jpg|png|gif
     *  2.校验文件是否为恶意程序  防止木马病毒.
     *  3.按照分目录结构 存储图片
     *  4.应该让文件名称不重复
     *
     * @param file
     * @return
     *
     */


    private String locald ="D:/user/software/images";
    //private String locald ="/usr/local/src/images";

    private String preurl="http://image.jt.com";



    @Override
    public ImageVO upload(MultipartFile file) {

        // String locald ="/usr/local/src/images";
        //1.校验图片的类型
        //1.1   获取图片名称   abc.gif ABC.GIF
         String fileName = file.getOriginalFilename();
        //1.2 将字母转化为小写
        fileName=fileName.toLowerCase();
        //1.3   正则表达式写法
        if(!fileName.matches("^.+\\.(jpg|png|gif)$")){
            return null;
        }

        System.out.println("是图片！");

        //2.校验文件是否为恶意程序  防止木马病毒.
        try {
            BufferedImage bufferedImage =
                    ImageIO.read(file.getInputStream());
            //读取图片  判断长度和宽度
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();

            if(width==0 || height==0){
                //说明 不是正经图片
                return null;
            }

            //3.分目录存储 提交的时间|名称hash abasedhh|type 进行存户
            String datePath= new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
            //3.1 创建图片目录
            String fileDir=locald +datePath;

            //3.2 创建目录
            File dirFile= new File(fileDir);
            if (!dirFile.exists()){
                dirFile.mkdirs();
            }

            //4.要求文件名称不重名  uuid.type
            String uuid = UUID.randomUUID().toString().replace("-","");
            int index= fileName.lastIndexOf(".");

            //.jpg
            String type= fileName.substring(index);
            fileName=uuid + type;

            //5.实现文件上传
            String realFilePath = fileDir+ fileName;
            file.transferTo(new File(realFilePath));

            //6.返回ImageVO对象  /2022/11/11/uuid.jpg
            String virtualPath = datePath + fileName;
            //http://image.jt.com/2022/11/11/uuid.jpg
            String urlPath=preurl+virtualPath;
            ImageVO imageVO = new ImageVO(virtualPath,urlPath,fileName);
            System.out.println(urlPath);
            return imageVO;

        } catch (IOException e) {
            e.printStackTrace();
            //如果出现了异常信息,则返回null即可
            return null;
        }

    }

    @Override
    public void deleteFile(String virtualPath) {
       // String locald ="D:/usr/software/images";
        String path=locald + virtualPath;
        File file1 = new File(path);
        if (file1.exists()){
            file1.delete();
        }


    }
}
